package com.coollang.smartbra.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.CountActionDetailsActivity;
import com.coollang.smartbra.beans.ActionData;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Auther: Timmy
 * @Data: 2017/5/17
 * @Fuction: 平板支撑详情数据
 */

public class CountActionDetailsAdapter extends RecyclerView.Adapter {

    private final Context mContext;
    private ArrayList<ActionData> mActionDatas;
    private static final int VIEW_CORRECT_ITEM = 0;
    private static final int VIEW_ERROR_ITEM = 1;

    public CountActionDetailsAdapter(Context context) {
        mContext = context;
        mActionDatas = new ArrayList<>();
    }

    @Override
    public int getItemCount() {
        return mActionDatas.size();
    }

    public ActionData getItem(int position) {
        return mActionDatas.get(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView;
        RecyclerView.ViewHolder holder = null;

        if (viewType == VIEW_CORRECT_ITEM) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_countaction_correct, parent, false);
            holder = new CorrectViewHolder(itemView);
        } else if (viewType == VIEW_ERROR_ITEM) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_countaction_error, parent, false);
            holder = new ErrorViewHolder(itemView);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);

        if (viewType == VIEW_CORRECT_ITEM) {
            ((CorrectViewHolder) holder).setData(position);
        } else if (viewType == VIEW_ERROR_ITEM) {
            ((ErrorViewHolder) holder).setData(position);
        }
    }

    //根据不同的数据返回不同的viewType
    @Override
    public int getItemViewType(int position) {
        int errorFlag = mActionDatas.get(position).getErrorFlag();
        if (errorFlag == 1) {
            return VIEW_CORRECT_ITEM;
        } else if (errorFlag == 2) {
            return VIEW_ERROR_ITEM;
        }
        return 0;
    }

    public void addData(ArrayList<ActionData> datas) {
        mActionDatas = datas;
        notifyDataSetChanged();
    }

    public int getItemPosition(ActionData data) {
        return mActionDatas.indexOf(data);
    }

    class CorrectViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_postion)
        TextView mTVPosition;

        private CorrectViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }

        public void setData(int position) {
            mTVPosition.setText(mActionDatas.size() - position + "");
        }
    }

    class ErrorViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_postion)
        TextView mTVPosition;
        @BindView(R.id.tv_errorinfo)
        TextView mTVErrorInfo;
        private int mPosition;

        private ErrorViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);

            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((CountActionDetailsActivity) mContext).onClickErrorItem(mPosition);
                }
            });
        }

        public void setData(int position) {
            mPosition = position;
            ActionData data = mActionDatas.get(position);
            mTVPosition.setText(mActionDatas.size() - position + "");
            mTVErrorInfo.setText(data.getErrorInfo());
        }
    }
}


